package gz.metro.datasync.service;

import gz.metro.datasync.entity.form.SyncLogForm;
import gz.metro.datasync.iservice.IDatasyncService;
import gz.metro.datasync.utils.JdbcHelper;
import gz.metro.datasync.utils.Result;
import gz.metro.datasync.utils.page.PageQueryRequest;
import gz.metro.datasync.utils.page.PagerVO;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author chenwx
 * @description 数据同步业务服务类
 * @createTime 2019-11-25 10:19
 */
@Slf4j
@Service
public class DatasyncService implements IDatasyncService {

    @Autowired
    private OrgSyncService orgSyncService;

    @Autowired
    private UserSyncService userSyncService;

    @Autowired
    private JdbcHelper jdbcHelper;


    /**
     * 执行组织全量同步
     *
     * @return 同步结果
     */
    @Transactional
    @Override
    public Result execOrgFullSync() {
        log.info("execOrgFullSync");

        //设置全量同步标识
        orgSyncService.setFullSync(true);
        //执行同步
        Result result = orgSyncService.syncData();

        return result;
    }

    /**
     * 执行组织增量同步
     *
     * @return 同步结果
     */
    @Transactional
    @Override
    public Result execOrgIncSync() {
        log.info("execOrgIncSync");

        //执行同步
        Result result = orgSyncService.syncData();

        return result;
    }

    /**
     * 执行用户全量同步
     *
     * @return 同步结果
     */
    @Override
    public Result execUserFullSync() {
        log.info("execUserFullSync");

        //设置全量同步标识
        userSyncService.setFullSync(true);
        //执行同步
        Result result = userSyncService.syncData();

        return result;
    }

    /**
     * 执行用户增量同步
     *
     * @return 同步结果
     */
    @Override
    public Result execUserIncSync() {
        log.info("execUserIncSync");

        //执行同步
        Result result = userSyncService.syncData();

        return result;
    }

    /**
     * 分页查询同步日志
     *
     * @param pageQueryRequest
     * @return
     */
    @Override
    public Result<PagerVO<SyncLogForm>> pageQuerySyncLog(PageQueryRequest pageQueryRequest) {
        log.info("pageQuerySyncLog");
        log.info("pageQueryRequest", pageQueryRequest);

        Result<PagerVO<SyncLogForm>> result = new Result<>();

        if (pageQueryRequest ==  null){
            pageQueryRequest = new PageQueryRequest();
        }

        int pageIndex = pageQueryRequest.getIndex();
        int pageSize = pageQueryRequest.getSize();
        String sort = pageQueryRequest.getSort();
        if(StringUtils.isBlank(sort)){
            sort = "beginTime desc";
        }

        try{
            //执行分页查询
            PageQueryRequest.Filters params = pageQueryRequest.getParams();
            String sql = "SELECT * FROM sync_log WHERE 1 = 1 ";
            //拼接sql语句
            sql = jdbcHelper.joinFilters(sql,params,sort);
            PagerVO<SyncLogForm> pagerVO = jdbcHelper.queryForPage(sql,pageIndex,pageSize, new BeanPropertyRowMapper<>(SyncLogForm.class));
            //设置分页查询结果
           result.setBusinessObject(pagerVO);
        }catch (Exception e){
            log.error("数据同步日志查询失败", e);
            result.setSuccess(false);
            result.setMessage(String.format("数据同步日志查询失败【{0}】", e.getMessage()));
        }

        return result;
    }
}
